// filename: string_utility.h
// author: enic
// date: 2015-11-30

#pragma once

#include <cstdint>
#include <string>
#include <vector>

namespace utility
{


	inline int32_t SplitString(__in std::wstring src, __in std::vector<std::wstring> _vecSpliter,
		__out std::vector<std::wstring> &_splitList)
	{
		if (src.empty() || _vecSpliter.empty())
		{
			return 0;
		}

		int iSrcLen = wcslen(src.c_str());
		int iSplitLen = _vecSpliter.size();

		int iPrevOccurIdx = 0;
		for (int i = 0; i < iSrcLen; i++)
		{
			for (int m = 0; m < iSplitLen; m++)
			{
				std::wstring _spliter = _vecSpliter[m];
				if (_wcsnicmp(_spliter.c_str(), src.c_str() + i, _spliter.length()))
				{
					continue;
				}

				int iCopyLen = i - iPrevOccurIdx + 1;
				if (iCopyLen > 1)
				{
					wchar_t *strMid = new wchar_t[i - iPrevOccurIdx + 1];
					memset(strMid, 0, iCopyLen * sizeof(wchar_t));
					wcsncpy(strMid, src.c_str() + iPrevOccurIdx, iCopyLen - 1);

					iPrevOccurIdx = i + _spliter.length();

					_splitList.push_back(strMid);

					delete[]strMid;
				}

				break;
			}

			if (iPrevOccurIdx >= iSrcLen)
			{
				break;
			}
		}

		if (iPrevOccurIdx < iSrcLen)
		{
			_splitList.push_back(src.c_str() + iPrevOccurIdx);
		}

		return _splitList.size();
	}

}